package redis_m

import (
	"context"
	"fmt"
	"github.com/go-redis/redis/v8"
	"my_chat/setting"
)

var (
	Client *redis.Client
	Nil    = redis.Nil
)

func Init(cfg *setting.RedisConfig) (err error) {
	Client = redis.NewClient(&redis.Options{
		Addr:     fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
		Password: cfg.Password, // no password set
		DB:       cfg.DB,       // use default DB
		PoolSize: cfg.PoolSize,
	})
	_, err = Client.Ping(context.Background()).Result()
	if err != nil {
		fmt.Println("初始化redis错误")
		return
	}
	return
}

func Close() {
	_ = Client.Close()
}
